<template>
  <el-card class="box-card">
    <div slot="header" class="clearfix">
      <span>轮播图</span>
      <el-button
        style="float: right; padding: 3px 10px"
        type="primary"
        size="normal"
        icon="el-icon-plus"
        @click="addHandle"
        >新增</el-button
      >
    </div>
    <el-form :inline="true" :model="formInline" class="demo-form-inline">
      <el-form-item label="关键词">
        <el-input v-model="formInline.user" placeholder=""></el-input>
      </el-form-item>
      <el-form-item label="分类">
        <el-select v-model="formInline.category" placeholder="分类">
          <el-option label="全部" value="" />
          <el-option
            v-for="item in categories"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">查询</el-button>
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="list" style="width: 100%">
      <el-table-column type="index" label="序号" width="180" align="center" />
      <el-table-column label="类别" align="center">
        <!-- slot-scope的可以获取数据 -->
        <template slot-scope="scope">
          <span>{{
            scope.row.category ? scope.row.category.name : '暂无'
          }}</span>
        </template>
      </el-table-column>
      <el-table-column label="主图" align="center">
        <!-- slot-scope的可以获取数据 -->
        <template slot-scope="scope">
          <img :src="scope.row.coverImage | imgUrl" />
        </template>
      </el-table-column>
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="handleEdit(scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            icon="el-icon-remove"
            type="danger"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- current-change本身有回调参数 触发事件中就不用写了 回调参数为当前页 -->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      @current-change="loadData"
    />
  </el-card>
</template>

<script>
import { loadBanners, delBanners } from '../../api/banner'
import { loadBannerstype } from '../../api/banners-category'
export default {
  data() {
    return {
      list: [],
      total: 0,
      loading: false,
      formInline: {
        user: '',
        category: ''
      },
      categories: []
    }
  },
  async created() {
    const res = await loadBannerstype()
    this.categories = [...res.data]
    this.loadData(1)
  },
  methods: {
    async loadData(page) {
      this.loading = true
      const res = await loadBanners({ page })
      this.loading = false
      this.list = res.data
      this.total = res.total
    },
    addHandle() {
      this.$router.push({
        name: 'BannersAdd'
      })
    },
    handleEdit(item) {
      this.$router.push({
        name: 'BannersAdd',
        query: {
          id: item.id
        }
      })
    },
    handleDelete(item) {
      delBanners(item.id).then(res => {
        this.$message.success('删除成功')
        this.loadData()
      })
    },
    async onSubmit() {
      this.loading = true
      const res = await loadBanners({
        name: this.formInline.user,
        category: this.formInline.category
      })
      this.loading = false
      this.list = res.data
      this.total = res.total
    }
  }
}
</script>

<style lang="scss" scoped>
img {
  width: 50%;
}
</style>
